const e = require('express');
var express = require('express');
var router = express.Router();
const studentService = require('../service/studentService');

router.get('/',async function(req,res){
    let {type,value,current,size,callback} = req.query;
    let searchObj = {};
    if(type){
        if(type == "age"){
            searchObj[type] = value;
        }else{
            searchObj[type] = {$regex:value};
        }
        
    }
    let data = await studentService.find(searchObj
            ,{current:current||1,size:size||5});
    res.send(data);
});
router.get('/:id',async function(req,res){
    let id = req.params.id;
    let data = await studentService.findById(id);
    res.send(data);
});
router.post('/',async function(req,res){
    let data = await studentService.insert(req.body);
    res.send(data);
});
router.put('/:id',async function(req,res){
    let id = req.params.id;
    if(!req.body.cls){
        delete req.body.cls;
    }
    let data = await studentService.update(id,req.body);
    res.send(data);
});
router.delete('/:id',async function(req,res){
    let data = await studentService.delete(req.params.id);
    res.send(data);
})

module.exports = router;